﻿using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Transforms;

[UpdateAfter(typeof(ShootAttackSystem))]
partial struct AnimationStateSystem : ISystem
{
    private ComponentLookup<ActiveAnimation> activeAnimationComponentLookUp;

    [BurstCompile]
    public void OnCreate(ref SystemState state)
    {
        activeAnimationComponentLookUp = state.GetComponentLookup<ActiveAnimation>(false);
    }

    [BurstCompile]
    public void OnUpdate(ref SystemState state)
    {
        activeAnimationComponentLookUp.Update(ref state);
        IdleWalkingAnimationStateJob idleWalkingAnimationStateJob = new IdleWalkingAnimationStateJob();
        idleWalkingAnimationStateJob.activeAnimationComponentLookUp = activeAnimationComponentLookUp;
        idleWalkingAnimationStateJob.ScheduleParallel();
        //foreach ((RefRW<AnimatedMesh> animatedMesh, RefRW<UnitMover> unitMover, RefRW<UnitAnimations> unitAnimations)
        //    in
        //    SystemAPI.Query<RefRW<AnimatedMesh>, RefRW<UnitMover>, RefRW<UnitAnimations>>())
        //{
        //    RefRW<ActiveAnimation> activeAnimation = SystemAPI.GetComponentRW<ActiveAnimation>(animatedMesh.ValueRO.meshEntity);
        //    if (unitMover.ValueRO.isMoving)
        //    {
        //        activeAnimation.ValueRW.nextAnimationType = unitAnimations.ValueRO.walkAnimationType;
        //    }
        //    else
        //    {
        //        activeAnimation.ValueRW.nextAnimationType = unitAnimations.ValueRO.idleAnimationType;
        //    }
        //}
        activeAnimationComponentLookUp.Update(ref state);
        AimShootAnimationStateJob aimShootAnimationStateJob = new AimShootAnimationStateJob();
        aimShootAnimationStateJob.activeAnimationComponentLookUp = activeAnimationComponentLookUp;
        aimShootAnimationStateJob.ScheduleParallel();

        //foreach ((RefRW<AnimatedMesh> animatedMesh, RefRO<UnitMover> unitMover, RefRO<Target> target, RefRW<ShootAttack> shootAttack, RefRW<UnitAnimations> unitAnimations)
        //in
        //SystemAPI.Query<RefRW<AnimatedMesh>, RefRO<UnitMover>, RefRO<Target>, RefRW<ShootAttack>, RefRW<UnitAnimations>>())
        //{
        //    if (unitMover.ValueRO.isMoving == false && target.ValueRO.tagertEntity != Entity.Null)
        //    {
        //        RefRW<ActiveAnimation> activeAnimation = SystemAPI.GetComponentRW<ActiveAnimation>(animatedMesh.ValueRO.meshEntity);
        //        activeAnimation.ValueRW.nextAnimationType = unitAnimations.ValueRO.aimAnimationType;
        //    }
        //    if (shootAttack.ValueRO.onShoot.isTriggered)
        //    {
        //        RefRW<ActiveAnimation> activeAnimation = SystemAPI.GetComponentRW<ActiveAnimation>(animatedMesh.ValueRO.meshEntity);
        //        activeAnimation.ValueRW.nextAnimationType = unitAnimations.ValueRO.shootAnimationType;
        //    }
        //}
        activeAnimationComponentLookUp.Update(ref state);
        MeleeAttackAnimationStateJob meleeAttackAnimationStateJob = new MeleeAttackAnimationStateJob();
        meleeAttackAnimationStateJob.activeAnimationComponentLookUp = activeAnimationComponentLookUp;
        meleeAttackAnimationStateJob.ScheduleParallel();

        //foreach ((RefRW<AnimatedMesh> animatedMesh, RefRW<MeleeAttack> meleeAttack, RefRW<UnitAnimations> unitAnimations)
        //in
        //SystemAPI.Query<RefRW<AnimatedMesh>, RefRW<MeleeAttack>, RefRW<UnitAnimations>>())
        //{
        //    if (meleeAttack.ValueRO.onAttack)
        //    {
        //        RefRW<ActiveAnimation> activeAnimation = SystemAPI.GetComponentRW<ActiveAnimation>(animatedMesh.ValueRO.meshEntity);
        //        activeAnimation.ValueRW.nextAnimationType = unitAnimations.ValueRO.meleeAttackAnimationType;
        //    }
        //}
    }

}

[BurstCompile]
public partial struct MeleeAttackAnimationStateJob : IJobEntity
{
    [NativeDisableParallelForRestriction] public ComponentLookup<ActiveAnimation> activeAnimationComponentLookUp;
    public void Execute(in AnimatedMesh animatedMesh, in MeleeAttack meleeAttack, in UnitAnimations unitAnimations)
    {
        if (meleeAttack.onAttack)
        {
            RefRW<ActiveAnimation> activeAnimation = activeAnimationComponentLookUp.GetRefRW(animatedMesh.meshEntity);
            activeAnimation.ValueRW.nextAnimationType = unitAnimations.meleeAttackAnimationType;
        }
    }
}

[BurstCompile]
public partial struct AimShootAnimationStateJob : IJobEntity
{
    [NativeDisableParallelForRestriction] public ComponentLookup<ActiveAnimation> activeAnimationComponentLookUp;
    public void Execute(in AnimatedMesh animatedMesh, in UnitMover unitMover, in Target target, in ShootAttack shootAttack, in UnitAnimations unitAnimations)
    {
        if (unitMover.isMoving == false && target.tagertEntity != Entity.Null)
        {
            RefRW<ActiveAnimation> activeAnimation = activeAnimationComponentLookUp.GetRefRW(animatedMesh.meshEntity);
            activeAnimation.ValueRW.nextAnimationType = unitAnimations.aimAnimationType;
        }
        if (shootAttack.onShoot.isTriggered)
        {
            RefRW<ActiveAnimation> activeAnimation = activeAnimationComponentLookUp.GetRefRW(animatedMesh.meshEntity);
            activeAnimation.ValueRW.nextAnimationType = unitAnimations.shootAnimationType;
        }
    }
}

[BurstCompile]
public partial struct IdleWalkingAnimationStateJob : IJobEntity
{
    [NativeDisableParallelForRestriction] public ComponentLookup<ActiveAnimation> activeAnimationComponentLookUp;
    public void Execute(in AnimatedMesh animatedMesh, in UnitMover unitMover, in UnitAnimations unitAnimations)
    {
        RefRW<ActiveAnimation> activeAnimation = activeAnimationComponentLookUp.GetRefRW(animatedMesh.meshEntity);
        if (unitMover.isMoving)
        {
            activeAnimation.ValueRW.nextAnimationType = unitAnimations.walkAnimationType;
        }
        else
        {
            activeAnimation.ValueRW.nextAnimationType = unitAnimations.idleAnimationType;
        }
    }
}